//
//  ZHMyAddressViewController.m
//  LoveFreshBee
//
//  Created by brice Mac on 2016/10/18.
//  Copyright © 2016年 王忠迪. All rights reserved.
//
#define bgColor [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1.0]

#import "ZHMyAddressViewController.h"
#import "LFNetworkTools.h"
#import "ZHMyAddressData.h"
#import "ZHMyAddressCell.h"
#import "AddaddrViewController.h"
#import "EditAddressViewController.h"

@interface ZHMyAddressViewController ()<ZHMyAddressCellDelegate>

@property(nonatomic,strong)NSString *code;

@property(nonatomic,strong)NSMutableArray *address;

@end

@implementation ZHMyAddressViewController

- (NSMutableArray *)address{
    if (_address == nil) {
        _address = [NSMutableArray array];
    }
    return _address;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupMyAddressUI];
}

/**
  请求“我的收货地址”数据
 */
- (void)loadData {
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:@"12" forKey:@"call"];
    
    [[LFNetworkTools sharedTools] POST_RequestWithUrlString:@"MyAdress.json.php" parmas:params successBloock:^(NSDictionary *responseObject) {
        
        //成功的回调
        //1. 记录登陆状态码
        self.code = responseObject[@"code"];
        //2. 取出返回的数据 -> 字典转模型
        NSArray *dataArray = responseObject[@"data"];
        
        for (NSDictionary *dic in dataArray) {
            ZHMyAddressData *model = [ZHMyAddressData modelWithDict:dic];
            [self.address addObject:model];
        }
        
        // 刷新tableView，数据显示
        [self.tableView reloadData];
        //  NSLog(@"%@",responseObject);
        
    } failureBlock:^(NSError *error) {
        
        //失败的回调
        NSLog(@"%@",error);
    }];
}

- (void)setupMyAddressUI {
    
    //设置行高
    self.tableView.rowHeight = 80;
    //设置背景颜色
    self.tableView.backgroundColor = bgColor;
    //加载 xib
    UINib *nib = [UINib nibWithNibName:@"ZHMyAddressCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"MyAddressCellId"];
    //设置 headerView 和 footerView 为可见的一个 UIView
    [self setupHeaderAndFooterView];
    
    //设置底部的按钮
    [self setupBottomView];
}

- (void)setupBottomView {
    
    UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-54, kScreenW, 54)];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.userInteractionEnabled = YES;
    [self.view addSubview:bottomView];
    
    UIButton *addButton = [[UIButton alloc]initWithFrame:CGRectMake(kScreenW/6, 10, kScreenW*2/3, 34)];
    addButton.layer.cornerRadius = 5;
    addButton.layer.masksToBounds = YES;
    [addButton setTitle:@"+新增地址" forState:UIControlStateNormal];
    [addButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [addButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addButton.backgroundColor = [UIColor colorWithRed:253/255.0 green:212/255.0 blue:50/255.0 alpha:1.0];
    //添加按钮点击事件
    [addButton addTarget:self action:@selector(addButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:addButton];
}

// 新增按钮被点击
- (void)addButtonDidClick {
    
    //跳转新增地址页面
    AddaddrViewController * add = [[AddaddrViewController alloc]init];
    
    add.callBackClick = ^(ZHMyAddressData *model) {
        //新增一个模型数据用于显示
        [self.address addObject:model];
        //刷新显示
        [self.tableView reloadData];
    };
    
    [self.navigationController pushViewController:add animated:YES];
}

- (void)setupHeaderAndFooterView {
    
    //设置tableView的头部灰色View
    UIView *headerBgColor = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 15)];
    self.tableView.tableHeaderView = headerBgColor;
    
    //设置tableView的尾部灰色View
    UIView *footerBgColor = [[UIView alloc]initWithFrame:self.tableView.bounds];
    self.tableView.tableFooterView = footerBgColor;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.address.count) {
        
        return self.address.count;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ZHMyAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyAddressCellId" forIndexPath:indexPath];
    
    cell.delegate = self;
    
    ZHMyAddressData *model = self.address[indexPath.row];
    model.index = indexPath.row;
    
    cell.model = model;
    
    return cell;
}

- (void)editButtonClick:(UIButton *)editButton {
    EditAddressViewController *edit = [[EditAddressViewController alloc]init];
    edit.model = self.address[editButton.tag];
    
    [self.navigationController pushViewController:edit animated:YES];
}


@end



